package com.example.month11.festivalsms;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.example.month11.festivalsms.bean.FestivalLab;
import com.example.month11.festivalsms.bean.Msg;
import com.example.recyclerview01.R;
import com.google.android.material.floatingactionbutton.FloatingActionButton;

import static com.example.month11.festivalsms.fragment.FestivalCategoryFragment.ID_FESTIVAL;

public class ChooseMsgActivity extends AppCompatActivity {
    private ListView mListView;
    private FloatingActionButton mFbsToSend;
    private ArrayAdapter<Msg> mAdapter;
    private int mFestivalId;
    private LayoutInflater mInflater;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_choose_msg);
        mFestivalId = getIntent().getIntExtra(ID_FESTIVAL, -1);
        mInflater = LayoutInflater.from(this);

        // 设置页面主标题。
        setTitle(FestivalLab.getInstance().getFestivalById(mFestivalId).getName());
        initViews();
        initEvent();
    }

    private void initEvent() {
        mFbsToSend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 没有选择短信，自己去编辑。
                SendMsgActivity.toActivity(ChooseMsgActivity.this, mFestivalId, -1);
            }
        });
    }

    private void initViews() {
        mListView = findViewById(R.id.id_lv_msgs);
        mFbsToSend = findViewById(R.id.id_fbs_send);

        mListView.setAdapter(mAdapter = new ArrayAdapter<Msg>(this, -1,
                FestivalLab.getInstance().getMsgsByFestivalId(mFestivalId)) {
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                // 复写getView方法
                if (convertView == null) {
                    convertView = mInflater.inflate(R.layout.item_msg, parent, false);
                }
                TextView tvContent = convertView.findViewById(R.id.id_tv_content);
                Button btnToSend = convertView.findViewById(R.id.id_btn_tosend);
                tvContent.setText(getItem(position).getContent());
                btnToSend.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        SendMsgActivity.toActivity(ChooseMsgActivity.this, mFestivalId, getItem(position).getId());
                    }
                });
                return convertView;
            }
        });
    }

}